package guiscreens;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Time;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import dao.ExecuteQuery;

/**
 * This class handles viewing items.
 * 
 * @author Lee
 * @version 1.0
 */
public class ItemFocus extends JPanel {

	private CardLayout c1;
	private JPanel basePanel;
	private JFrame thisFrame;
	private List<String> list;
	private JTextField textField;
	private String itemID;
	private String currentBid;
	private int bidcounter = 0;
	private int getNowCounter = 0;
	private String iname;
	private String min;
	private boolean getNowInEffect;
	private double getItNowPrice;

	/**
	 * Create the panel.
	 */
	public ItemFocus(final String itemID, final String currentBid, final CardLayout c1, final JPanel basePanel, 
			final JFrame thisFrame) {
		this.itemID = itemID;
		this.c1 = c1;
		this.basePanel = basePanel;
		this.thisFrame = thisFrame;
		this.currentBid = currentBid;

		thisFrame.setBounds(login.XCENTER - 325, login.YCENTER - 300, 650, 600);
		list = ExecuteQuery.viewItem(Integer.parseInt(itemID));
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(6dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(147dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(71dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(60dlu;default)"),},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(17dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(29dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(21dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(15dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(63dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		thisFrame.setTitle("GTBay Item For Sale");
		setBackground(Color.WHITE);

		JLabel lblItemId = new JLabel("Item ID");
		add(lblItemId, "4, 2");

		JLabel lblNewLabel = new JLabel(itemID);
		add(lblNewLabel, "8, 2");

		JLabel lblViewRatings = new JLabel("<html><u>View Ratings</u></html>");
		lblViewRatings.addMouseListener(new MouseListener(){
			public void mouseClicked(MouseEvent e) {
				ItemRatings ratings = new ItemRatings(itemID, list.get(0), c1, basePanel, thisFrame);
				basePanel.add(ratings, "RATINGSCARD");
				c1.show(basePanel, "RATINGSCARD");
			}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}	
		});
		add(lblViewRatings, "10, 2, left, default");

		JLabel lblItemName = new JLabel("Item Name");
		add(lblItemName, "4, 4");

		iname = list.get(0);

		JLabel lblNewLabel_1 = new JLabel(iname);
		add(lblNewLabel_1, "8, 4");

		JLabel lblDescription = new JLabel("Description");
		add(lblDescription, "4, 6, default, top");

		JTextArea textArea = new JTextArea();
		add(textArea, "8, 6, left, fill");
		textArea.setEditable(false);
		textArea.append(list.get(1));

		if (list.get(7).equals(login.username))
		{
			JLabel lblEditDescription = new JLabel("<html><u>Edit Description</u></html>");
			lblEditDescription.addMouseListener(new MouseListener()
			{
				public void mouseClicked(MouseEvent e) {
					String description = JOptionPane.showInputDialog(null, "Please enter the new description");
					ExecuteQuery.editDescription(description, Integer.parseInt(itemID));
					JOptionPane.showMessageDialog(null, "Description changed!");
				}
				public void mousePressed(MouseEvent e) {}
				public void mouseReleased(MouseEvent e) {}
				public void mouseEntered(MouseEvent e) {}
				public void mouseExited(MouseEvent e) {}	
			});
			add(lblEditDescription, "10, 8, left, default");
		}

		JLabel label_11 = new JLabel("");
		add(label_11, "10, 6");

		JLabel lblCategory = new JLabel("Category");
		add(lblCategory, "4, 10");

		JLabel label = new JLabel(list.get(2));
		add(label, "8, 10");

		JLabel lblReturnsAccepted = new JLabel("Returns Accepted?");
		add(lblReturnsAccepted, "4, 12");

		JCheckBox checkBox = new JCheckBox();
		checkBox.setBackground(Color.WHITE);
		if (Integer.parseInt(list.get(4)) == 1) checkBox.setSelected(true);
		add(checkBox, "8, 12");

		if (list.get(5) != null) {
			getNowInEffect=true;
			JLabel lblGetItNow = new JLabel("Get It Now Price");
			add(lblGetItNow, "4, 14");

			JPanel panel = new JPanel();
			panel.setBackground(Color.WHITE);
			add(panel, "8, 14, left, fill");

			JLabel label_2 = new JLabel("$");
			panel.add(label_2);

			getItNowPrice = Double.parseDouble(list.get(5));
			JLabel lblNewLabel_2 = new JLabel(list.get(5));
			panel.add(lblNewLabel_2);

			JButton btnNewButton = new JButton("Get It Now!");
			btnNewButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if (getNowCounter == 0)
					{
						ExecuteQuery.getNow(Integer.parseInt(itemID), login.username, Double.parseDouble(list.get(5)));
						getNowCounter++;
						JOptionPane.showMessageDialog(null, "Item bought!");
					}
					else JOptionPane.showMessageDialog(null, "Er... didn't you just buy this item?");
				}
			});
			panel.add(btnNewButton);
		}

		JLabel lblAuctionEnds = new JLabel("Auction Ends:");
		add(lblAuctionEnds, "4, 16");

		JLabel lblNewLabel_3 = new JLabel(list.get(6));
		add(lblNewLabel_3, "8, 16");

		JLabel lblLatestBids = new JLabel("Latest Bids");
		add(lblLatestBids, "4, 18");
		Border pb = BorderFactory.createLineBorder(Color.BLACK, 1);
		try {
		}
		catch(IndexOutOfBoundsException e) {}

		JPanel panel_1 = new JPanel();
		add(panel_1, "4, 20, 8, 1, fill, fill");
		panel_1.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));

		JLabel lblBidAmount = new JLabel("<html><u>Bid Amount</html></u>");
		panel_1.add(lblBidAmount, "4, 2");

		JLabel lblTiemOfBid = new JLabel("<html><u>Time of Bid</u></html>");
		panel_1.add(lblTiemOfBid, "14, 2");

		JLabel lblUsername = new JLabel("<html><u>Username</u></html>");
		panel_1.add(lblUsername, "24, 2");
		try {
			JLabel lblNewLabel_4 = new JLabel(list.get(9));
			panel_1.add(lblNewLabel_4, "4, 4");

			JLabel label_1 = new JLabel("          ");
			panel_1.add(label_1, "8, 4");

			JLabel label_3 = new JLabel(list.get(10));
			panel_1.add(label_3, "14, 4");

			JLabel label_2 = new JLabel("          ");
			panel_1.add(label_2, "18, 4");

			JLabel label_7 = new JLabel(list.get(11));
			panel_1.add(label_7, "24, 4");

			JLabel lblNewLabel_5 = new JLabel(list.get(12));
			panel_1.add(lblNewLabel_5, "4, 6");

			JLabel label_4 = new JLabel(list.get(13));
			panel_1.add(label_4, "14, 6");

			JLabel label_8 = new JLabel(list.get(14));
			panel_1.add(label_8, "24, 6");

			JLabel lblNewLabel_6 = new JLabel(list.get(15));
			panel_1.add(lblNewLabel_6, "4, 8");

			JLabel label_5 = new JLabel(list.get(16));
			panel_1.add(label_5, "14, 8");

			JLabel label_9 = new JLabel(list.get(17));
			panel_1.add(label_9, "24, 8");

			JLabel lblNewLabel_7 = new JLabel(list.get(18));
			panel_1.add(lblNewLabel_7, "4, 10");

			JLabel label_6 = new JLabel(list.get(19));
			panel_1.add(label_6, "14, 10");

			JLabel label_10 = new JLabel(list.get(20));
			panel_1.add(label_10, "24, 10");

		} catch(IndexOutOfBoundsException e) {}

		JLabel lblYourBid = new JLabel("Your bid:");
		add(lblYourBid, "4, 22");

		JPanel panel_3 = new JPanel();
		panel_3.setBackground(Color.WHITE);
		add(panel_3, "8, 22, left, fill");

		JLabel label_1 = new JLabel("$");
		panel_3.add(label_1);

		textField = new JTextField();
		panel_3.add(textField);
		textField.setColumns(10);

		min = list.get(8);
		try {
			if (Double.parseDouble(list.get(8)) < Double.parseDouble(currentBid)) min = currentBid;
		} catch(NullPointerException e) {}
		JLabel lblminimumBid = new JLabel("(minimum bid " + (Double.parseDouble(min) + 1) + ")");
		add(lblminimumBid, "8, 24");

		JPanel panel = new JPanel();
		add(panel, "8, 26, right, fill");

		JButton btnBidOnThis = new JButton("Bid on this item!");
		panel.add(btnBidOnThis);
		panel.setBackground(Color.WHITE);
		btnBidOnThis.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				String bid = textField.getText();
				try {
					if (list.get(7).equals(login.username))
					{
						JOptionPane.showMessageDialog(null, "You cannot bid on your own items!");
					}
					else if (getNowCounter != 0) 
					{
						JOptionPane.showMessageDialog(null, "You bought this item already.  Stop bidding on it!");
					}
					else if (getNowInEffect && Double.parseDouble(bid) > getItNowPrice)
					{
						JOptionPane.showMessageDialog(null, "Why are you bidding a greater amount than the get it now price?!!!");
					}

					else if (Double.parseDouble(bid) < Double.parseDouble(min) + 1)
					{
						JOptionPane.showMessageDialog(null, "Please enter a greater bid");
					}

					else if (bidcounter != 0)
					{
						JOptionPane.showMessageDialog(null, "Please refresh before bidding again.");
					}

					else 
					{
						ExecuteQuery.placeABid(login.username, bid, itemID);
						JOptionPane.showMessageDialog(null, "Bid placed");
						bidcounter++;
					}
				}
				catch(NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Please enter a monetary amount for your bid");
				}
			}
		});

		JButton btnCancel = new JButton("Cancel");
		add(btnCancel, "10, 26");
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				ResultSet results = ExecuteQuery.searchItem(SearchItem.keyword, SearchItem.category, SearchItem.minimumPrice, SearchItem.maximumPrice, SearchItem.condition);
				SearchResults resultsPanel = new SearchResults(c1, basePanel, thisFrame, results);
				basePanel.add(resultsPanel, "NEWRESULTSCARD");
				c1.show(basePanel, "NEWRESULTSCARD");
			}
		});

		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		add(panel_2, "4, 28, 9, 1, right, fill");

		JButton btnSubmit = new JButton("Submit");
		panel_2.add(btnSubmit);

	}

}
